﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Eassets.Service.Global;
using Eassets.Layered.Domain.Global;
using Eassets.Service.Asset;
using Eassets.Layered.Domain.Asset;
using System.Collections.ObjectModel;
using Eassets.Service;
using System.Text.RegularExpressions;

namespace Eassets.UI.Asset
{
    public partial class ChangeAssetCustodianForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GetOrganizationLevelList();
                GetAllMainCategory();
            }

        }

        private void GetOrganizationLevelList()
        {
            dropDownListOrganizationLevelList.Items.Clear();

            OrganizationLevelListService organizationLevelListService = new OrganizationLevelListService();
            List<OrganizationLevelList> organizationLevelLists = new List<OrganizationLevelList>();
            organizationLevelLists = organizationLevelListService.GetAllOrganizationLevelList().Where(x => x.OrganizationLevelMode == "B").ToList();
            dropDownListOrganizationLevelList.DataSource = organizationLevelLists;
            dropDownListOrganizationLevelList.DataValueField = "OrganizationLevelListId";
            dropDownListOrganizationLevelList.DataTextField = "Code";
            dropDownListOrganizationLevelList.DataBind();
        }

        private void GetAllMainCategory()
        {
            dropDownListMainCategory.Items.Clear();
            CategoryService categoryService = new CategoryService();
            Category category = new Category();
            try
            {
                dropDownListMainCategory.DataSource = categoryService.GetAllAssetCategory().Where(x => x.ParentId == 0).ToList();

                dropDownListMainCategory.DataValueField = "CategoryId";
                dropDownListMainCategory.DataTextField = "Description";
                dropDownListMainCategory.DataBind();

            }
            catch (Exception exception)
            {
                throw;
            }
        }

        private void GetAssets()
        {
            dropDownListAsset.Items.Clear();
            AssetService assetService = new AssetService();
            Assets assets = new Assets();
            try
            {
                dropDownListAsset.DataSource = assetService.GetAllAsset().Where(x => x.AssetCategoryId == Convert.ToInt32(dropDownListSubCategory.SelectedValue)).ToList();

                dropDownListAsset.DataValueField = "AssetId";
                dropDownListAsset.DataTextField = "Description";
                dropDownListAsset.DataBind();
            }
            catch (Exception exception)
            {
                throw;
            }
        }

        private void GetAllSubCategory()
        {
            dropDownListSubCategory.Items.Clear();
            CategoryService categoryService = new CategoryService();
            Category category = new Category();
            try
            {
                dropDownListSubCategory.DataSource = categoryService.GetAllAssetCategory().Where(x => x.ParentId == Convert.ToInt32(dropDownListMainCategory.SelectedValue)).ToList();

                dropDownListSubCategory.DataValueField = "CategoryId";
                dropDownListSubCategory.DataTextField = "Description";
                dropDownListSubCategory.DataBind();
            }
            catch (Exception exception)
            {
                throw;
            }
        }


        protected void DataBindToGridView()
        {
            List<AssetItemDetails> assetItemDetails = new List<AssetItemDetails>();
            AssetItemDetailsService assetItemDetailsService = new AssetItemDetailsService();
            assetItemDetails = assetItemDetailsService.GetAllAssetItemDetails().Where(x => x.AssetId == Convert.ToInt64(dropDownListAsset.SelectedValue)).ToList();
            if (dropDownListEmployee.SelectedValue != "")
            {
                assetItemDetails = (from AssetItemDetail in assetItemDetails where AssetItemDetail.AssetStatus != "DIS" && AssetItemDetail.EmployeeID == Convert.ToInt64(dropDownListEmployee.SelectedValue) select AssetItemDetail).ToList();
            }

            gridViewAssetDetails.DataSource = assetItemDetails;
            gridViewAssetDetails.DataBind();
        }

        protected Collection<AssetItemDetails> GetFixedAssetItemDetailsByAssetId(long assetId)
        {
            Collection<AssetItemDetails> assetItemDetails = new Collection<AssetItemDetails>();
            AssetItemDetailsService assetItemDetailsService = new AssetItemDetailsService();
            assetItemDetails = assetItemDetailsService.GetFixedAssetItemDetailsByAssetId(assetId); ;
            return assetItemDetails;
        }

        protected void GetAllEmployee()
        {
            dropDownListNewOwner.Items.Clear();
            EmployeeService employeeService = new EmployeeService();
            if(dropDownListEmployee.SelectedValue!="")
            {
             dropDownListNewOwner.DataSource = employeeService.GetAllEmployee().Where(x => x.EmployeeType == "Employee" && x.EmployeeId != Convert.ToInt64(dropDownListEmployee.SelectedValue));
            }
           
            dropDownListNewOwner.DataTextField = "EmployeeName";
            dropDownListNewOwner.DataValueField = "EmployeeId";
            dropDownListNewOwner.DataBind();
        }

        protected void buttonChange_OnClick(object sender, EventArgs e)
        {
            try
            {
                TransactionService transactionService = new TransactionService();
                FixedAssetCustodianService fixedAssetCustodianService = new FixedAssetCustodianService();
                Transaction transaction = new Transaction();
                transaction.TransactionTypeCode = Convert.ToString("ASCUS");
                transaction.TransactionDocumentCode = Convert.ToString("ACS");
                transaction.CompanyId = 0;
                transaction.OrganizationLevelListId = Convert.ToInt64(dropDownListOrganizationLevelList.SelectedValue);
                int serial = Getserial();
                dropDownListTransactionNo.SelectedItem.Text = Convert.ToString(CreateNewTransactionCode((serial + 1)));
                dropDownListTransactionNo.Enabled = false;
                //transaction.TransactionNo = "GAM/PRN/20120830/000001";
                transaction.TransactionNo = Convert.ToString(CreateNewTransactionCode((serial + 1)));
                transaction.TransactionUserId = 1;
                transaction.TransactionStatus = "A";
                //transaction.TransactionDate = DateTime.Now;


                transactionService.AddTransaction(transaction);

                long transactionId = transactionService.GetMaxTransactionNumber();


                //for (int i = 0; i < assetProcurementDetails.TransactionLine; i++)
                //{
                FixedAssetCustodian fixedAssetCustodian = new FixedAssetCustodian();

                fixedAssetCustodian.TransactionId = transactionId;
                fixedAssetCustodian.OrganizationLevelListId = Convert.ToInt64(dropDownListOrganizationLevelList.SelectedValue);
                fixedAssetCustodian.CompanyId = 1;
                fixedAssetCustodian.AssetId = Convert.ToInt64(dropDownListAsset.SelectedValue);
                ViewState["AssetId"] = fixedAssetCustodian.AssetId;
                fixedAssetCustodian.ItemSerialCode = ViewState["SerialCode"].ToString();
                ViewState["AssetSerialCode"] = fixedAssetCustodian.ItemSerialCode;
                fixedAssetCustodian.PastOwner = Convert.ToString(dropDownListEmployee.SelectedValue);
                fixedAssetCustodian.NewOwner = Convert.ToString(dropDownListNewOwner.SelectedValue);

                fixedAssetCustodianService.AddFixedAssetCustodian(fixedAssetCustodian);

                //Get the SerialCode to be Disposal

                AssetItemDetailsService assetItemDetailsService = new AssetItemDetailsService();
                AssetItemDetails assetItemDetails = new AssetItemDetails();
                assetItemDetails.AssetId = Convert.ToInt64(dropDownListAsset.SelectedValue);
                assetItemDetails.ItemSerial = ViewState["AssetSerialCode"].ToString();
                assetItemDetails.AssetStatus = Convert.ToString("ACS");
                assetItemDetailsService.UpdateAssetItemDetailsStatus(assetItemDetails);


                AssetItemDetails assetItemDetail = new AssetItemDetails();
                assetItemDetail.AssetId = Convert.ToInt64(ViewState["AssetId"]);
                assetItemDetail.ItemSerial = ViewState["AssetSerialCode"].ToString();
                assetItemDetailsService.UpdateCustodian(assetItemDetail);

                //Update TransactionSerial Table
                TransactionSerialService transactionSerialService = new TransactionSerialService();
                transactionSerialService.UpdateTransactionSerial("ACS");
            }
            catch (Exception)
            {

            }
        }

        private int Getserial()
        {
            long transactionSerial = 0;
            TransactionSerialService transactionSerialService = new TransactionSerialService();

            transactionSerial = transactionSerialService.GetMaxTransactionSerialByWorkFlowDocumentCode("ACS");
            return Convert.ToInt16(transactionSerial);
        }

        private String CreateNewTransactionCode(int maxSerial)
        {


            // int maxSerial = (Convert.ToInt16(transactionSerialDomain.Serial)+1);
            string maxSerialFormated;
            maxSerialFormated = CreateNumberFormat(Convert.ToInt16(maxSerial));
            Regex regex = new Regex("-");
            string locationCode = dropDownListOrganizationLevelList.SelectedItem.Text;
            string[] arrayParameters = regex.Split(locationCode);
            String yearMonth = DateTime.Now.ToString("yyyyMM");

            string OrganizationLevelListCode = Convert.ToString(arrayParameters[0]);

            string NewTransactionCode = (OrganizationLevelListCode + "/"
                                     + "ACS" + "/"
                                     + yearMonth + "/"
                                   + maxSerialFormated);
            return NewTransactionCode;
        }

        private String CreateNumberFormat(Int16 maxSerial)
        {
            string maxSerialCode;
            if ((maxSerial / 10) == 0)
            {
                maxSerialCode = "0000" + maxSerial;
            }
            else if ((maxSerial / 100) == 0)
            {
                maxSerialCode = "000" + maxSerial;
            }
            else if ((maxSerial / 1000) == 0)
            {
                maxSerialCode = "00" + maxSerial;
            }
            else if ((maxSerial / 10000) == 0)
            {
                maxSerialCode = "0" + maxSerial;
            }
            else
            {
                maxSerialCode = maxSerial.ToString();
            }
            return maxSerialCode;
        }

        protected void buttonConfirm_OnClick(object sender, EventArgs e)
        {

        }

        protected void OrganizationLevelList_SelectedIndexChange(object sender, EventArgs e)
        {

        }


        protected void dropDownListMainCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetAllSubCategory();
        }

        protected void dropDownListSubCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetAssets();
        }

        protected void dropDownListAsset_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetEmployeeByAssetId();
            DataBindToGridView();
        }

        protected void GetEmployeeByAssetId()
        {
            dropDownListEmployee.Items.Clear();
            List<AssetItemDetails> assetItemDetails = new List<AssetItemDetails>();
            assetItemDetails = GetFixedAssetItemDetailsByAssetId(Convert.ToInt64(dropDownListAsset.SelectedValue)).ToList();

            AssetItemDetailsService assetItemDetailsService = new AssetItemDetailsService();
            var list = new Dictionary<string, string>();

            foreach (
                AssetItemDetails assetItemDetail in
                    assetItemDetails)
            {
                list.Add(assetItemDetail.EmployeeID.ToString(),
                         assetItemDetail.Employee.EmployeeName);
            }

            dropDownListEmployee.DataSource = list;
            dropDownListEmployee.DataTextField = "Value";
            dropDownListEmployee.DataValueField = "Key";
            dropDownListEmployee.DataBind();

        }

        protected void dropDownEmployee_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataBindToGridView();
        }

        protected void dropDownListEmployee_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void dropDownListNewOwner_SelectedIndexChanged(object sender, EventArgs e)
        {
            //dropDownListNewOwner.Items.Clear();
            //EmployeeService employeeService = new EmployeeService();
            //dropDownListNewOwner.DataSource = employeeService.GetAllEmployee().ToList();
            //dropDownListNewOwner.DataTextField = "EmployeeName";
            //dropDownListNewOwner.DataValueField = "EmployeeId";
            //dropDownListNewOwner.DataBind();
        }

        protected void dropDownEffectiveDate_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        //protected void gridViewAssetDetails_PageIndexChanging(object sender, GridViewPageEventArgs e)
        //{

        //}

        protected void gridViewAssetDetails_SelectedIndexChange(object sender, EventArgs e)
        {
            //GridViewRow row = gridViewAssetDetails.SelectedRow;
            //ViewState["SerialCode"] = row.Cells[3].Text.Trim();
            GetAllEmployee();
            //textBoxSuggestedValue.Text = row.Cells[6].Text.Trim();

        }
    }
}